package com.dianping.cat.consumer.top.model.transform;
import com.dianping.cat.consumer.top.model.IVisitor;
import com.dianping.cat.consumer.top.model.entity.Domain;
import com.dianping.cat.consumer.top.model.entity.Error;
import com.dianping.cat.consumer.top.model.entity.Machine;
import com.dianping.cat.consumer.top.model.entity.Segment;
import com.dianping.cat.consumer.top.model.entity.TopReport;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  17 */     this.m_linker = new DefaultLinker(true);




/*  22 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

/*  26 */   public static TopReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static TopReport parse(InputStream in) {
/*  30 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    TopReport topReport = new TopReport();

    try {
/*  34 */       topReport.accept(parser);
/*  35 */     } catch (RuntimeException e) {
      if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  39 */         throw e;
      }
    }

/*  43 */     parser.m_linker.finish();
/*  44 */     return topReport;
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  51 */     while ((tag = readTag()) != -1) {
/*  52 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  57 */     switch (_field) {
      case 1:
/*  59 */         domain.setName(readString());
        break;
      case 33:
/*  62 */         if (_type == 1) {
/*  63 */           Segment segment = new Segment();

/*  65 */           visitSegment(segment);
/*  66 */           this.m_linker.onSegment(domain, segment); break;
/*  67 */         }  if (_type == 2) {
/*  68 */           for (int i = readInt(); i > 0; i--) {
/*  69 */             Segment segment = new Segment();

/*  71 */             visitSegment(segment);
/*  72 */             this.m_linker.onSegment(domain, segment);
          }
        }
        break;
    }
  }



  public void visitError(Error error) {
    byte tag;
/*  83 */     while ((tag = readTag()) != -1) {
/*  84 */       visitErrorChildren(error, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitErrorChildren(Error error, int _field, int _type) {
/*  89 */     switch (_field) {
      case 1:
/*  91 */         error.setId(readString());
        break;
      case 2:
/*  94 */         error.setCount(readInt());
        break;
    }
  }



  public void visitMachine(Machine machine) {
    byte tag;
/* 103 */     while ((tag = readTag()) != -1) {
/* 104 */       visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/* 109 */     switch (_field) {
      case 1:
/* 111 */         machine.setId(readString());
        break;
      case 2:
/* 114 */         machine.setCount(readInt());
        break;
    }
  }



  public void visitSegment(Segment segment) {
    byte tag;
/* 123 */     while ((tag = readTag()) != -1) {
/* 124 */       visitSegmentChildren(segment, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitSegmentChildren(Segment segment, int _field, int _type) {
/* 129 */     switch (_field) {
      case 1:
/* 131 */         segment.setId(Integer.valueOf(readInt()));
        break;
      case 2:
/* 134 */         segment.setError(readLong());
        break;
      case 3:
/* 137 */         segment.setUrl(readLong());
        break;
      case 4:
/* 140 */         segment.setUrlDuration(readDouble());
        break;
      case 5:
/* 143 */         segment.setService(readLong());
        break;
      case 6:
/* 146 */         segment.setServiceDuration(readDouble());
        break;
      case 7:
/* 149 */         segment.setSql(readLong());
        break;
      case 8:
/* 152 */         segment.setSqlDuration(readDouble());
        break;
      case 9:
/* 155 */         segment.setCall(readLong());
        break;
      case 10:
/* 158 */         segment.setCallDuration(readDouble());
        break;
      case 11:
/* 161 */         segment.setCache(readLong());
        break;
      case 12:
/* 164 */         segment.setCacheDuration(readDouble());
        break;
      case 13:
/* 167 */         segment.setUrlError(readLong());
        break;
      case 14:
/* 170 */         segment.setUrlSum(readDouble());
        break;
      case 15:
/* 173 */         segment.setServiceError(readLong());
        break;
      case 16:
/* 176 */         segment.setServiceSum(readDouble());
        break;
      case 17:
/* 179 */         segment.setSqlError(readLong());
        break;
      case 18:
/* 182 */         segment.setSqlSum(readDouble());
        break;
      case 19:
/* 185 */         segment.setCallError(readLong());
        break;
      case 20:
/* 188 */         segment.setCallSum(readDouble());
        break;
      case 21:
/* 191 */         segment.setCacheError(readLong());
        break;
      case 22:
/* 194 */         segment.setCacheSum(readDouble());
        break;
      case 33:
/* 197 */         if (_type == 1) {
/* 198 */           Error error = new Error();

/* 200 */           visitError(error);
/* 201 */           this.m_linker.onError(segment, error); break;
/* 202 */         }  if (_type == 2) {
/* 203 */           for (int i = readInt(); i > 0; i--) {
/* 204 */             Error error = new Error();

/* 206 */             visitError(error);
/* 207 */             this.m_linker.onError(segment, error);
          }
        }
        break;
      case 34:
/* 212 */         if (_type == 1) {
/* 213 */           Machine machine = new Machine();

/* 215 */           visitMachine(machine);
/* 216 */           this.m_linker.onMachine(segment, machine); break;
/* 217 */         }  if (_type == 2) {
/* 218 */           for (int i = readInt(); i > 0; i--) {
/* 219 */             Machine machine = new Machine();

/* 221 */             visitMachine(machine);
/* 222 */             this.m_linker.onMachine(segment, machine);
          }
        }
        break;
    }
  }



  public void visitTopReport(TopReport topReport) {
    byte tag;
/* 233 */     if ((tag = readTag()) != -4) {
/* 234 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 237 */     while ((tag = readTag()) != -1) {
/* 238 */       visitTopReportChildren(topReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitTopReportChildren(TopReport topReport, int _field, int _type) {
/* 243 */     switch (_field) {
      case 1:
/* 245 */         topReport.setDomain(readString());
        break;
      case 2:
/* 248 */         topReport.setStartTime(readDate());
        break;
      case 3:
/* 251 */         topReport.setEndTime(readDate());
        break;
      case 33:
/* 254 */         if (_type == 1) {
/* 255 */           Domain domain = new Domain();

/* 257 */           visitDomain(domain);
/* 258 */           this.m_linker.onDomain(topReport, domain); break;
/* 259 */         }  if (_type == 2) {
/* 260 */           for (int i = readInt(); i > 0; i--) {
/* 261 */             Domain domain = new Domain();

/* 263 */             visitDomain(domain);
/* 264 */             this.m_linker.onDomain(topReport, domain);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 273 */       return new Date(readVarint(64));
/* 274 */     } catch (IOException e) {
/* 275 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 281 */       return Double.longBitsToDouble(readVarint(64));
/* 282 */     } catch (IOException e) {
/* 283 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 289 */       return (int)readVarint(32);
/* 290 */     } catch (IOException e) {
/* 291 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 297 */       return readVarint(64);
/* 298 */     } catch (IOException e) {
/* 299 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 305 */       return this.m_in.readUTF();
/* 306 */     } catch (IOException e) {
/* 307 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 313 */       return this.m_in.readByte();
/* 314 */     } catch (IOException e) {
/* 315 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 320 */     int shift = 0;
/* 321 */     long result = 0L;

/* 323 */     while (shift < length) {
/* 324 */       byte b = this.m_in.readByte();
/* 325 */       result |= (b & 0x7F) << shift;
/* 326 */       if ((b & 0x80) == 0) {
/* 327 */         return result;
      }
/* 329 */       shift += 7;
    }

/* 332 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/top/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */